package edu.xalead.版本四;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class Windows extends JFrame {
    Method m = new Method();
    char[][] board = m.createBoard();
    char var = '0';
    public Windows() throws HeadlessException {
        //通过构造函数来初始化窗口
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口的同时关闭虚拟机
        this.setSize(800,600); //设置窗口大小
        this.setLocation(500,100);//设置窗口位置
        this.setVisible(true); //设置为可见的
        this.setResizable(false);//设置为不可改变大小
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                if (e.getX() < 570 && e.getY() < 570) {
//                    int a = core.ChessIt(_CgetX(e.getX()), (_CgetY(e.getY())), var);
                    int a = m.playChess(board,_CgetX(e.getX()),_CgetY(e.getY()));
                    Windows.this.repaint(); //重绘
                    if (a == '0') {
                        JOptionPane.showMessageDialog(null,"白棋赢了", "恭喜", JOptionPane.DEFAULT_OPTION);;
                    }
                    if(a=='1') {
                        JOptionPane.showMessageDialog(null,"黑棋赢了", "恭喜", JOptionPane.DEFAULT_OPTION);;
                    }
                }

            }
        });
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        // 横
        for (int i = 0; i < 19; i++)
            g.drawLine(30, 30 + i * 30, 570, 30 + i * 30);
        // 竖线
        for (int i = 0; i < 19; i++)
            g.drawLine(30 + i * 30, 60, 30 + i * 30, 570);

        //遍历数组棋盘，画出相应的棋子
        for (int i = 0; i < 19; i++) {
            for (int j = 0; j < 19; j++) {
                if (board[i][j] =='0')
                    g.drawOval(20 + i * 30, 50 + j * 30, 20, 20);
                if(board[i][j]=='1')
                    g.fillOval(20+i*30, 50+j*30, 20, 20);
            }
        }
        g.drawRect(690,60, 50, 30);
        g.drawString("悔棋",700,80);
        g.drawRect(690,120,50, 30);
        g.drawString("开始",700,140);
        g.drawRect(690,180,50, 30);
        g.drawString("设置",700,200);

    }

    private int _CgetX(int x) {
        x -= 30;
        if (x % 15 <= 7)
            return x / 30;
        else
            return x / 30 + 1;
    }

    private int _CgetY(int y) {
        y -= 60;
        if (y % 15 <= 7)
            return y / 30;
        else
            return y / 30 + 1;
    }
}
